import { CommontListResp, PageReqParams } from '@/api/types'
import request, { ApiResponse } from '@/utils/service/request'
import { CommonUserInfo } from './model/accountModel'
import {
  AddOrganizationReq,
  EditOrganizationReq,
  FindOrgMemberReq,
  OrganizationItem,
  UpdateOrgMemberReq,
} from './model/organizationModel'

export enum OrganizationApi {
  /** 查询组织部门列表 */
  GET_ORGANIZATION_LIST = '/user-center/organization/getTreeOrgans',
  /** 增加部门 */
  ADD_ORGANIZATION = '/user-center/organization/insert',
  /** 删除部门 */
  DELETE_ORGANIZATION = '/user-center/organization/delete',
  /** 编辑部门信息 */
  EDIT_ORGANIZATION = '/user-center/organization/update',
  /** 找到组织下的人员 */
  FIND_ORG_MEMBER = '/user-center/organization/findUserByOrganId',
  /** 更新组织架构人员关系 */
  UPDATE_ORG_MEMBER = '/user-center/organization/updateOrganUsersRel',
}

export function fetchOrgList() {
  return request.get<ApiResponse<OrganizationItem[]>>(
    OrganizationApi.GET_ORGANIZATION_LIST
  )
}

export function addOrg(body: AddOrganizationReq) {
  return request.post<ApiResponse<null>>(OrganizationApi.ADD_ORGANIZATION, body)
}

export function delOrg(orgId: string) {
  return request.post<ApiResponse<null>>(OrganizationApi.DELETE_ORGANIZATION, {
    id: orgId,
  })
}

export function editOrg(body: EditOrganizationReq) {
  return request.post<ApiResponse<null>>(
    OrganizationApi.EDIT_ORGANIZATION,
    body
  )
}

export function findOrgMember(
  orgId: string,
  pageInfo: PageReqParams = { size: -1, current: 1 }
) {
  const params: FindOrgMemberReq = {
    id: orgId,
    ...pageInfo,
  }
  return request.get<ApiResponse<CommontListResp<CommonUserInfo> | null>>(
    OrganizationApi.FIND_ORG_MEMBER,
    {
      params,
    }
  )
}

export function updateOrgMenber(body: UpdateOrgMemberReq) {
  return request.post<ApiResponse<null>>(
    OrganizationApi.UPDATE_ORG_MEMBER,
    body
  )
}
